<template>
  <!-- 修改病历 -->
  <el-dialog title="修改病历" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <blForm ref="blForm" :dataForm="dataForm" :cureFirstVisitRecord="cureFirstVisitRecord" :doctorId="doctorId" :deptId="deptId" @submit="onSubmitForm"></blForm>
    <span slot="footer">
      <!-- <el-button type="danger" :loading="btnLoading" @click="onDel">删除历史病例</el-button> -->
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "cureId", "doctorId", "deptId"],
  components: {
    blForm: () => import("../_jz/_com/blForm.vue")
  },
  data() {
    return {
      btnLoading: false,
      dataForm: [],
      cureFirstVisitRecord: [],
      form: {
        id: "",
        cureId: "",
        cureDynamicId: "",
        dynamicTemplateId: "",
        dynamicMedical: "",
        doctorId: ""
      }
    };
  },
  methods: {
    onSubmitForm(obj) {
      const th = this;
      th.btnLoading = true;
      th.form.dynamicMedical = JSON.stringify(obj);
      th.$api
        .bladdOrUpdate(th.form)
        .then(res => {
          if (res.executed) {
            th.$message.success("保存成功");
            th.onClose();
          } else {
            th.$message.error(res.message);
          }
          th.btnLoading = false;
        })
        .catch(res => {
          th.btnLoading = false;
        });
    },
    onSubmit() {
      const th = this;
      th.$refs["blForm"].onSubmit();
    },
    onDel() {},
    getData() {
      const th = this;
      let j = { cureId: th.cureId };
      th.$api.getcureDynamicdetailid(j).then(res => {
        if (res.executed) {
          if (res.data.cureFirstVisitRecord)
            th.cureFirstVisitRecord = res.data.cureFirstVisitRecord;
          if (res.data.cureMedical) { 
            let cureMedical = res.data.cureMedical;
            th.dataForm = cureMedical.templateDynamicFieldBeans;
            th.form.cureDynamicId = cureMedical.id;
            th.form.dynamicTemplateId = cureMedical.templateId;
          } else {
            th.$message.warning("该患者没有病例信息，不能修改！");
            th.onClose();
          }
        } else {
          th.$message.warning("该患者没有病例信息，不能修改！");
          th.onClose();
        }
      });
    },
    onOpen() {
      const th = this;
      th.form.cureId = th.cureId;
      th.form.doctorId = th.doctorId;
      th.getData();
    },
    onClose() {
      const th = this;
      th.$emit("input", false);
    }
  }
};
</script>